using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;
using CE.Provider;
using CE.User;
using CE.Mail;

public partial class Admin_provider_SendProviderConfirmation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
            {
                Response.Redirect("AdminSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("AdminSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            DataTable _dtProviderType = General.GetComboItem("TH.ProviderType", false);
            cblProviderType.DataSource = _dtProviderType;
            cblProviderType.DataTextField = _dtProviderType.Columns[0].ColumnName.ToString();
            cblProviderType.DataValueField = _dtProviderType.Columns[1].ColumnName.ToString();
            cblProviderType.DataBind();

            DataTable _dtCompany = General.GetComboItem("TH.ProviderCompany", false);
            ddlProviderCompanyName.DataSource = _dtCompany;
            ddlProviderCompanyName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
            ddlProviderCompanyName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
            ddlProviderCompanyName.DataBind();

            ddlProviderCompanyName.Items.Insert(0, new ListItem("Select", "-1"));
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        //if (ddlProviderId.SelectedValue != "-1")
        //{
        //    Account _account = new Account();
        //    AccountDB _accountdb = new AccountDB();
        //    Int64 _ceid = Convert.ToInt64(ddlProviderId.SelectedValue);
        //    string _guid = _accountdb.GetGUID(_ceid);
        //    if (_guid != null)
        //    {
        //        _account = _accountdb.GetAccount(_ceid);
        //        string _message = "Your provider account has been created!<br><br>";
        //        string _email = lblProviderEmailAddress.Text;
        //        string _subject = "Provider Account Activation - ComparExpress.com";
        //        string _confirmpath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ConfirmProviderPath");
        //        _message += "<b>Email Address : </b>" + _email + "<br>";
        //        _message += "<b>Password : </b>" + _account.AuthCode.ToString() + "<br>";
        //        _message += "<b>You have been assigned following functionality.</b><br>";
        //        _message += lblCheckList.Text + "<br><br>";
        //        if (MyMailManager.SendConfirmation(_email,_subject,_message,_guid,_confirmpath))
        //        {
        //            lblMessage.Text = "Message has been sent!";
        //        }
        //        else
        //        {
        //            lblMessage.Text = "Error sending message!";
        //        }
        //    }
        //    else
        //    {
        //        lblMessage.Text = "Not found provider Id!";
        //    }
        //}

    }
    protected void ddlProviderCompanyName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlProviderCompanyName.SelectedValue != "-1")
        {
            ProviderCompanyLinkDB _procomdb = new ProviderCompanyLinkDB();
            DataTable _dtProvider = new DataTable("ProviderList");
            DataTable _dtList = new DataTable();
            _dtList.Columns.Add("Name");
            _dtList.Columns.Add("Email");

            DataTable _dtIDList = new DataTable();
            _dtIDList.Columns.Add("Name");
            _dtIDList.Columns.Add("CEID");

            _dtProvider = _procomdb.GetProviderList(Convert.ToInt64(ddlProviderCompanyName.SelectedValue));
            for (int i = 0; i < _dtProvider.Rows.Count; i++)
            {
                ProfileDB _profiledb = new ProfileDB();
                Profile _profile = new Profile();
                _profile = _profiledb.GetProfile(Convert.ToInt64(_dtProvider.Rows[i]["CEID"].ToString()));

                Contact _contact = new Contact();
                ContactDB _contactdb = new ContactDB();
                _contact = _contactdb.GetContact(Convert.ToInt64(_dtProvider.Rows[i]["CEID"].ToString()));

                if (_profile != null && _contact != null)
                {
                    DataRow _dtRow = _dtList.NewRow();
                    _dtRow["Name"] = _profile.Salutation + " " + _profile.SurName + " " + _profile.FirstName;
                    _dtRow["Email"] = _contact.Email;
                    _dtList.Rows.Add(_dtRow);

                    DataRow _dtIDRow = _dtIDList.NewRow();
                    _dtIDRow["Name"] = _profile.Salutation + " " + _profile.SurName + " " + _profile.FirstName;
                    _dtIDRow["CEID"] = _dtProvider.Rows[i]["CEID"].ToString();
                    _dtIDList.Rows.Add(_dtIDRow);
                }

                _profiledb = null;
                _contact = null;
            }
            ddlProviderName.DataSource = null;
            ddlProviderName.DataSource = _dtList;
            ddlProviderName.DataTextField = "Name";
            ddlProviderName.DataValueField = "Email";
            ddlProviderName.DataBind();

            ddlProviderId.DataSource = null;
            ddlProviderId.DataSource = _dtIDList;
            ddlProviderId.DataTextField = "Name";
            ddlProviderId.DataValueField = "CEID";
            ddlProviderId.DataBind();

            ddlProviderName.Items.Insert(0, new ListItem(" ", "-1"));
            ddlProviderId.Items.Insert(0, new ListItem(" ", "-1"));
            lblProviderEmailAddress.Text = "";
        }
        else
        {
            ddlProviderName.DataSource = null;
            lblProviderEmailAddress.Text = "";

        }
    }
    protected void ddlProviderName_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblProviderEmailAddress.Text = ddlProviderName.SelectedValue;
        ddlProviderId.SelectedIndex = ddlProviderName.SelectedIndex;

        //Clear CheckBox list check item
        foreach (ListItem lstItem in cblProviderType.Items)
        {
            lstItem.Selected = false;
        }
        btnSend.Enabled = false;
        //Check Item
        if (ddlProviderId.SelectedValue != "-1")
        {
            DataTable _dtList = new DataTable("ProviderTypeList");
            ProviderTypeDB _protypedb = new ProviderTypeDB();
            _dtList = _protypedb.GetAssignmentList(Convert.ToInt64(ddlProviderCompanyName.SelectedValue), Convert.ToInt64(ddlProviderId.SelectedValue));
            lblCheckList.Text = "None";
            btnSend.Enabled = true;
            if (_dtList != null)
            {
                if (_dtList.Rows.Count > 0)
                {
                    lblCheckList.Text = "<ul>";
                    foreach (DataRow rValue in _dtList.Rows)
                    {
                        foreach (ListItem lstItem in cblProviderType.Items)
                        {
                            if (rValue[0].ToString() == lstItem.Value)
                            {

                                lblCheckList.Text += "<li>" + lstItem.Text + "</li>";
                                lstItem.Selected = true;
                                break;
                            }
                        }
                    }
                    lblCheckList.Text += "</ul>";
                }
            }
        }
        else
        {
            lblProviderEmailAddress.Text = "";
            lblCheckList.Text = "None";
        }
    }
    protected void btnHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("../AdminHome.aspx");
    }
}
